package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.base.MyFragmentPagerAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.findbank.BankNetFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.findbank.BankPhoneFragment;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 找银行
 * Created by zad
 * 2017/1/10 13:47
 */
public class FindBankActivity extends BaseActivity {
    private static final int COLOR_NORMAL = 0xff999999;
    private static final int COLOR_SELECTED = 0xffffab18;
    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.tv_outlets)
    TextView tvOutlets;//附近网点
    @BindView(R.id.tv_bank_phone)
    TextView tvBankPhone;//银行电话

    /**
     * ViewPager容器
     */
    @BindView(R.id.id_vp_container)
    ViewPager mViewPager;

    private List<BaseFragment> mBaseFragments;

    private String bankName = "";

    @Override
    protected void initView(Bundle savedInstanceState) {
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        bankName = getIntent().getStringExtra("bankName");

        initViewPager();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_find_bank;
    }


    private void toggleSelectText(int selectedPosition) {
        mViewPager.setCurrentItem(selectedPosition);
        if (selectedPosition == 0) {
            //附近网点选中
            tvOutlets.setTextColor(COLOR_SELECTED);
            tvBankPhone.setTextColor(COLOR_NORMAL);
        } else {
            //银行电话选中
            tvOutlets.setTextColor(COLOR_NORMAL);
            tvBankPhone.setTextColor(COLOR_SELECTED);
        }
    }


    @OnClick({R.id.tv_outlets, R.id.tv_bank_phone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_outlets:
                toggleSelectText(0);
                break;
            case R.id.tv_bank_phone:
                toggleSelectText(1);
                break;
        }
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        if (mBaseFragments == null) {
            mBaseFragments = getFragments();
        }
        MyFragmentPagerAdapter mAdapter =
                new MyFragmentPagerAdapter(getSupportFragmentManager(), mBaseFragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(new MyPageChangeListener());
    }

    /**
     * 获取Fragment
     */
    private List<BaseFragment> getFragments() {
        List<BaseFragment> mFragments = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putBoolean("fromFromBank", true);
        BankNetFragment bankNetFragment=new BankNetFragment();
        bankNetFragment.setBuriedInfo(mPrePageId);
        bankNetFragment.setBankName(bankName);
        bankNetFragment.setArguments(bundle);
        mFragments.add(bankNetFragment);
        BankPhoneFragment bankPhoneFragment=new BankPhoneFragment();
        bankPhoneFragment.setBuriedInfo(mPrePageId);
        mFragments.add(bankPhoneFragment);
        return mFragments;
    }

    /**
     * ViewPager滑动监听
     */
    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            //切换标题
            toggleSelectText(position);
            if (onResumed) {
                countUmengPage(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    boolean onResumed;

    protected void onResume() {
        super.onResume();
        onResumed = true;
        if (mViewPager != null) {
            int position = mViewPager.getCurrentItem();
            countUmengPage(position);
        }
        GoogleAnalyticsUtil.setScreenName(Google.NEARBY_BANK);
    }

    @Override
    protected void onPause() {
        super.onPause();
        onResumed = false;
    }

    @Override
    protected void onDestroy() {
        int currentShow=mViewPager.getCurrentItem();
        if(currentShow==0){
            mCurrentPageId= Self.Page.app_repayment_banks_show;
        }else if(currentShow==1){
            mCurrentPageId=Self.Page.app_bank_tel_page;
        }
        super.onDestroy();
    }

    private void countUmengPage(int position) {
        if (position == 0) {
            //网点 ATM event_0x013
            UmengEventUtils.pageEvent(mContext, getString(R.string.event_0x013));
        } else {
            //银行电话 event_0x014
            UmengEventUtils.pageEvent(mContext, getString(R.string.event_0x014));
        }
    }
}
